$(document).ready(function() {
	//表格生产
    var machine_table=$('#machine_tb').DataTable({
		"language": { "url": root+"/resources/assets/Chinese.json"},
	    "processing": true,
	    "serverSide": true,
	    "ajax": {
		    "url": root+"/machine/itemView",
		    "contentType": "application/json",
		    "type": "POST",
		    "data": function (d) {
		      var jsonObj = {"start":d.start, "length":d.length,"draw":d.draw,"search":d.search,"columns":d.columns,'order':d.order[0]};
		      return JSON.stringify(jsonObj);
		    }
	  	},
	  	//表格列
	    "columns": [
	         { "title":"编号","data": "id","bSortable": false,"width":"50px","visible":true},
			 { "title":"机器名称","data": "name","bSortable": false,'width':'100px',"visible": true},
			 { "title":"商家名称","data": "shopName","bSortable": false,'width':'150px',"visible": true},
			 { "title":"创建时间","data": "createTime","bSortable": true,'width':'150px',"visible": true},
			 { "title":"结束时间","data": "endMD","bSortable": false,"visible": true},
			 { "title":"状态","data": "state.statusStr","bSortable": false ,"width":"70px"},
			 { "title":"操作项","data": "oper.operStr","bSortable": false,"width":"150px"}
	    ],
	    "fnDrawCallback": function(){
	        
	    	var api = this.api();
            var startIndex= api.context[0]._iDisplayStart;//获取到本页开始的条数
            api.column(0).nodes().each(function(cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
            
	    },
	   "columnDefs": [
             {
	        	 "render": function ( data, type, row ) {
					 var html=row.oper.operStr;
					 //html+='<a  id="'+row.id+'" class="btn btn-minier btn-success btn-order" href="'+root+'/entity/list?machineId='+row.id+'">表管理</a>';
					 return html;
				 }, "targets": 6
			 }
	     ],
		"order": [[3, "desc" ]],
	    "sScrollY": "100%",
	    "headerCallback":function(){
			$.get(root+"/machine/search",function(data){
				$('#machine_tb_length').parent('.col-xs-6').parent('.row').html(data);
			});
		}
    });
    //条件搜索
    $(document.body).on('click','#machine_search',function(){
      var data= $("#machine_search_form").serializeJson();
      machine_table.search(JSON.stringify(data)).draw();
	});
    //条件搜索重置
    $(document.body).on('click','#machine_reset',function(){
    	location.reload();
	});
    //删除
	$(document.body).on('click','.m-del',function(){
		$.post(root+'/machine/delete',{id:this.id},function(data){
			if(data.result==0){
				gritterAlert({class_name:'success',text:'删除成功！'});
				machine_table.ajax.reload( null, false );
			}else{
				gritterAlert({class_name:'error',text:data.message});
			}
		});
	});
	//启用停用
	$(document.body).on('click','.m-open-stop',function(){
		$.post(root+'/machine/updateStatus',{id:this.id},function(data){
			if(data.result==0){
				gritterAlert({class_name:'success',text:'操作成功！'});
				machine_table.ajax.reload( null, false );
			}else{
				gritterAlert({class_name:'error',text:data.message});
			}
		});
	});
	//编辑内容行
	$(document.body).on('click','.m-edit',function(evt){
		var modal=superFrameModal({
			'src':root+'/machine/show?id='+this.id,
			'width':900,
			'height':550,
			'title':'编辑',
			'faClass':'fa-pencil-square-o'
		});
		modal.show();
	});
	//新建内容
	$(document.body).on('click','#machine_new',function(evt){
		var modal=superFrameModal({
			'src':root+'/machine/new',
			'width':900,
			'height':550,
			'title':'新建',
			'faClass':'fa-user-plus'
		});
		modal.show();
	});
	//导出excel
	$(document.body).on('click','#machine_outEcxcel',function(){
		var path = root+"/machine/excelOut";
		$('#machine_search_form').append("<input type='hidden' name='campClassId'  value='"+$('#campClassId').val()+"'/>");
	    $('#machine_search_form').attr("action", path).submit();
	});

});
